home *** CD-ROM | disk | FTP | other *** search
- Path: news.uni-mannheim.de!news
- From: luebbeke@zew.de (Michael Luebbeke)
- Newsgroups: comp.lang.tcl,comp.unix.programmer,comp.unix.questions,comp.unix.shell,comp.lang.awk,comp.lang.c,comp.lang.perl.misc,comp.programming,comp.software-eng
- Subject: Re: Appropriate Tools and Approach for Kill Application
- Date: Wed, 17 Apr 1996 19:51:41 GMT
- Organization: ZEW Zentrum f. Europ. Wirtsch.-Forschung
- Message-ID: <4l2idm$j80@trumpet.uni-mannheim.de>
- References: <Pine.SOL.3.91.960417003525.18392A-100000@sun22>
- Reply-To: luebbeke@zew.de
- NNTP-Posting-Host: mlu.zew.de
- X-Newsreader: Forte Free Agent 1.0.82
-
- mjw@pobox.com wrote:
-
-
-
- >i.e.
-
- >kill -n x*
-
- >would kill xterm, xrn, xmosaic, etc.
-
- Got smthng, quick&dirty programmed, but maybe a start for you:
-
- Its from my early programming days, so don┤t wonder ┤bout
- root hacking without the use of popular modules like getopt.....
- its always spitting out, that processes weren`t killed,
- but they are, just ognore it.
-
- If anyone is interested, I will do some effort to nicen this stuff up.
- email me......
-
- -----------------------8<------------------------------>8-----------------------------------------------------
- #!/usr/bin/perl
- # perl script to kill a spcific program
- #
-
- $query = 0;
- $mode = -1;
- $verbose = 0;
- $pattern = "";
-
-
- for ($in = 0; $in < @ARGV; $in++)
- {
- if ($ARGV[$in] =~ /[-\/]v/)
- {
- $verbose = 1;
- }
- if ($ARGV[$in] =~ /[-\/]q/)
- {
- $query= 1;
- }
- if ($ARGV[$in] =~ /[-\/]ma/)
- {
- $mode = 0;
- }
- if ($ARGV[$in] =~ /[-\/]mr/)
- {
- $mode = 1;
- }
- if ($ARGV[$in] =~ /[-\/]mt/)
- {
- $mode = 15;
- }
- if ($ARGV[$in] =~ /[-\/]mk/)
- {
- $mode = 9;
- }
- if ($ARGV[$in] =~ /[-\/][Pp]/)
- {
- $pattern = $ARGV[$in+1];
- if ($verbose == 1)
- {
- print "Looking for pattern: <$pattern>\n";
- }
- }
- }
-
- if ((@ARGV == 0) || (mode == -1) || ($pattern eq ""))
- {
- print "KILL: killing processes\nusage: kill [-q] [-v] -m[r|t|k|a] -p
- pattern\n";
- print "\t\t-m : r=restart(SIGHUP) t=terminate(SIGTERM)
- k=kill(SIGKILL) a = try all\n";
- print "\t\t-q=query\n\t\t-v=verbose\n ";
- exit;
- }
-
- open(PS, "ps -ax |");
-
- while (<PS>)
- {
- chop($_);
- $_ =~ s/^\s*//;
- $_ =~ s/\s*$//;
- $_ =~ s/ +/,/o;
- $_ =~ s/ +/,/o;
- $_ =~ s/ +/,/o;
- $_ =~ s/ +/,/o;
- @procs = ($pid, $tty, $t, $time, $name) = split(/,/, $_);
- &kill_pattern($pattern, $pid, $name, $time, $tty);
- }
-
- sub kill_pattern
- {
- ($pattern, $pid, $name, $time, $tty) = @_;
-
- if ($$ eq $pid)
- {
- return;
- }
-
- if ($name =~ /$pattern/)
- {
- if (($query ==1) || ($verbose == 1))
- {
- print "Kill <$name> PID <$pid> Time <$time> TTY <$tty> ";
- }
- if ($query == 1)
- {
- print " Should I kill it? ";
- if (<STDIN> =~ /[NnQq]/)
- {
- return;
- }
- }
-
- if ($mode == 0)
- {
- print "Sending HUP ...";
- if ((kill(1 , $pid) == 0) || (&test_kill($pid) == 0))
- {
- print "Sending TERM ...";
- if ((kill(15 , $pid) == 0) || (&test_kill($pid) == 0))
- {
- print "Sending KILL ...";
- if ((kill(9 , $pid) == 0) || (&test_kill($pid) == 0))
- {
- print " can't kill it.\n";
- return;
- }
- }
- print " ... OK.\n";
- }
- }
- else
- {
- print "Sending Signal <$mode> ...";
- if ((kill($mode, $pid) == 0) || (&test_kill($pid) == 0))
- {
- print " ... no success.\n";
- return;
- }
- print " ... OK.\n";
- }
- }
- return;
- }
-
- sub test_kill
- {
- $p = $_[0];
- open(TEST, "ps -p $p |");
- @t = <TEST>;
- close(TEST);
-
- foreach $i (@t)
- {
- if ($i =~ /^\s*$p/)
- {
- return 0;
- }
- }
- return 1;
- }
-
-
-